package wan.com.templateproject.service.impl;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import wan.com.templateproject.common.BaseResponse;
import wan.com.templateproject.entity.People;
import wan.com.templateproject.hander.SendReddisHander;
import wan.com.templateproject.mapper.PeopleMapper;
import wan.com.templateproject.service.PeopleService;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wan
 * @version 1.0.0
 * @Description people业务层实现类
 * @createTime 2021年08月17日 14:14:46
 */
@Slf4j
@Service
public class PeopleServiceImpl implements PeopleService {

    /**
     * 通过名字进行注入
     */
    @Resource
    private PeopleMapper peopleMapper;

    /**
     * 通过类型注入
     */
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public BaseResponse<Boolean> sendRedis(Integer num) throws InterruptedException {

        //redisson锁
        RLock lock = redissonClient.getLock("redisLock");
        boolean boo = lock.tryLock(60, TimeUnit.SECONDS);
        if (boo) {
            try {
                List<People> peoples = peopleMapper.getPeople(null, null, num);
                peoples.forEach(p -> {
                    threadPoolExecutor.execute(new SendReddisHander("p.getName()", String.valueOf(1), redissonClient));
                });
            } catch (Exception e) {
                return new BaseResponse(500, e.getMessage());
            } finally {
                if (boo) {
                    lock.unlock();
                }
            }
        }
        return new BaseResponse(true);
    }

    @Override
    public BaseResponse<Boolean> sendRabbitmq(String msg) {
        threadPoolExecutor.execute(() -> {
            rabbitTemplate.convertAndSend("hello", msg);
        });
        return new BaseResponse<>(true);
    }

    @Override
    public BaseResponse<Boolean> updatePeople(People people) {
//        try {
//            peopleMapper.addPeople(people);
//            throw new Exception();
//        }catch (Exception e) {
//            log.info("发生异常：{}"+e.getMessage());
//        }
        redisTemplate.opsForValue().set("key1", "1231213");
        redisTemplate.opsForValue().set("key2", "1231213", 10000, TimeUnit.SECONDS);
        return new BaseResponse();
    }

    @Override
    public BaseResponse<PageInfo<People>> getPeoples(Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
        List<People> peoples = peopleMapper.getPeoples();
//        List<People> peoples = Arrays.asList();
//        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306:/demo", "root", "123123");
//        Statement statement = connection.createStatement();
//        ResultSet result = statement.executeQuery("SELECT id, name, age FROM t_people");
//        while (result.next()) {
//            int id = result.getInt("id");
//            String name = result.getString("name");
//            int age = result.getInt("age");
//            People people = People.builder()
//                    .id(id)
//                    .name(name)
//                    .age(age)
//                    .build();
//            peoples.add(people);
//        }
        return new BaseResponse(new PageInfo<>(peoples));
    }

    @Override
    public BaseResponse<Boolean> test1() {
        log.info("new/class/extends/public/private/protected/int .../static/finally/final");
        return null;
    }

    public static void main(String[] args) throws IOException {
        People people = new People("张三", 22, 1);
//        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\edy\\Desktop\\1.txt")));
//        String s = people.toString();
//        log.info("people对象：{}", s);
//        byte[] bytes = s.getBytes();
//        bos.write(bytes);
//        bos.close();
//        log.info("写出完成");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("C:\\Users\\edy\\Desktop\\1.txt")));
        oos.writeObject(people);
        log.info("写出完成");
    }
}
